﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.SceneControl;
using PloobsEngine.Physics;


namespace PloobsEngine.IA
{
    public class TriVisibilitySensor : ISensor<IList<VisibilitySensorInformation>>
    {
        private IWorld world;
        private IObject obj;
        private float altura = 25;        
        private float raysAngles = 50;
        private float leftSize = 50;

        public float LeftSize
        {
            get { return leftSize; }
            set { leftSize = value; }
        }
        private float rightSize = 100;

        public float RightSize
        {
            get { return rightSize; }
            set { rightSize = value; }
        }
        private float centerSize = 150;

        public float CenterSize
        {
            get { return centerSize; }
            set { centerSize = value; }
        }

        public float RaysAngles
        {
            get { return raysAngles; }
            set { raysAngles = value; }
        }


        public float Altura
        {
            get { return altura; }
            set { altura = value; }
        }
        private float distanciaDoCentroDeMassa = 10;

        public float DistanciaDoCentroDeMassa
        {
            get { return distanciaDoCentroDeMassa; }
            set { distanciaDoCentroDeMassa = value; }
        }

        public TriVisibilitySensor(IWorld world, IObject obj)
        {
            this.world = world;
            this.obj = obj;
        }
        #region ISensor Members

        public IList<VisibilitySensorInformation> DetectEnviroment()
        {
            List<VisibilitySensorInformation> l = new List<VisibilitySensorInformation>();
            Vector3 dir = obj.PhysicObject.FaceVector;            
            Ray raio = new Ray(obj.Position + new Vector3(0, altura, 0) + dir * distanciaDoCentroDeMassa, dir);

            Vector3 dir4 = Vector3.Transform(obj.PhysicObject.FaceVector, Matrix.CreateRotationY(MathHelper.ToRadians(-raysAngles)));
            Ray raio4 = new Ray(obj.Position + new Vector3(0, altura, 0) + dir4 * distanciaDoCentroDeMassa, dir);

            Vector3 dir5 = Vector3.Transform(obj.PhysicObject.FaceVector, Matrix.CreateRotationY(MathHelper.ToRadians(raysAngles)));
            Ray raio5 = new Ray(obj.Position + new Vector3(0, altura, 0) + dir5 * distanciaDoCentroDeMassa, dir);


            ///////So pra debug Remover isso depois            
            //DebugVectorDraw.addVector(raio.Position, dir * (centerSize - distanciaDoCentroDeMassa), Color.Red);
            //DebugVectorDraw.addVector(raio.Position, dir4 * (leftSize - distanciaDoCentroDeMassa), Color.Red);
            //DebugVectorDraw.addVector(raio.Position, dir5 * (rightSize - distanciaDoCentroDeMassa), Color.Red);
            //////


            {
                RayTestInfo ri = world.SegmentIntersect(raio, SegmentInterceptMethod.ALL, centerSize);
                if (ri == null)
                {
                }
                else
                {

                    if (ri.Object.Agente == null)
                    {
                        VisibilitySensorInformation si = new VisibilitySensorInformation();
                        //si.Agente = new NotAgent(ri.Object);
                        //si.RayTestInfo = ri;
                        //l.Add(si);
                    }
                    else
                    {
                        VisibilitySensorInformation si = new VisibilitySensorInformation();
                        si.Agente = ri.Object.Agente;
                        si.RayTestInfo = ri;
                        l.Add(si);
                    }
                    
                }
            }

                {
                    RayTestInfo ri = world.SegmentIntersect(raio4, SegmentInterceptMethod.ALL, leftSize);
                    if (ri == null)
                    {
                    }
                    else
                    {

                        if (ri.Object.Agente == null)
                        {
                            VisibilitySensorInformation si = new VisibilitySensorInformation();
                            //si.Agente = new NotAgent(ri.Object);
                            //si.RayTestInfo = ri;
                            //l.Add(si);
                        }
                        else
                        {
                            VisibilitySensorInformation si = new VisibilitySensorInformation();
                            si.Agente = ri.Object.Agente;
                            si.RayTestInfo = ri;
                            l.Add(si);
                        }

                    }
                }
                {
                    RayTestInfo ri = world.SegmentIntersect(raio5, SegmentInterceptMethod.ALL, rightSize);
                    if (ri == null)
                    {
                    }
                    else
                    {
                        if (ri.Object.Agente == null)
                        {
                            VisibilitySensorInformation si = new VisibilitySensorInformation();
                            //si.Agente = new NotAgent(ri.Object);
                            //si.RayTestInfo = ri;
                            //l.Add(si);
                        }
                        else
                        {
                            VisibilitySensorInformation si = new VisibilitySensorInformation();
                            si.Agente = ri.Object.Agente;
                            si.RayTestInfo = ri;
                            l.Add(si);
                        }
                    }
                }
                
                
                return l;


            }
        }

        #endregion

        
}
